package com.jingdianjichi.subject.handler;

import com.jingdianjichi.subject.common.enums.IsDeleteEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.convert.SubjectJudgeConverter;
import com.jingdianjichi.subject.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.entity.SubjectInfoBO;
import com.jingdianjichi.subject.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.impl.SubjectJudgeServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 判断题目的策略类
 */
@Component
public class JudgeTypeHandler implements  SubjectTypeHandler{

    private final SubjectJudgeServiceImpl subjectJudgeService;

    public JudgeTypeHandler(SubjectJudgeServiceImpl subjectJudgeService) {
        this.subjectJudgeService = subjectJudgeService;
    }

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        if(CollectionUtils.isEmpty(subjectInfoBO.getOptionList())) return;
        //判断题目的插入
        List<SubjectJudge> subjectJudges=new LinkedList<>();
        //遍历选项列表
        subjectInfoBO.getOptionList().forEach(option->{
            //将选项BO转换为选项实体
            SubjectJudge subjectJudge  = SubjectJudgeConverter.Instatnce.convertBOToRadio(option);
            //设置题目ID

            subjectJudge.setSubjectId(subjectInfoBO.getId());
            //设置是否删除
            subjectJudge.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
            //将选项实体添加到列表中
            subjectJudges.add(subjectJudge);
        });
        //插入单选题目的表
        subjectJudgeService.batchInsert(subjectJudges);
    }

    @Override
    public SubjectOptionBO query(Long id) {
        //获取判断
        List<SubjectJudge> subjectJudges= subjectJudgeService.queryBySubjectId(id);
        //将获取的信息 封装到OptionBo中
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        List<SubjectAnswerBO> subjectAnswerBOS = new ArrayList<>();
        subjectJudges.forEach(subjectJudge -> {
            SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
            subjectAnswerBO.setIsCorrect(subjectJudge.getIsCorrect());
            subjectAnswerBOS.add(subjectAnswerBO);
        });
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
